var t = wx.createCanvasContext("myCanvas");

Page({
    data: {
        colorArr: [ "black", "whitesmoke", "red", "orange", "yellow", "green", "yellowgreen", "blue", "cyan", "purple", "gray" ],
        drawStyle: "pencil",
        currentColor: "black",
        size: 3
    },
    startX: 0,
    startY: 0,
    isClear: !1,
    onLoad: function(e) {
        var s = wx.getSystemInfoSync().windowWidth, n = wx.getSystemInfoSync().windowHeight;
        t.setFillStyle("white"), t.fillRect(0, 0, .9 * s, n), t.draw();
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    chooseColor: function(t) {
        var e = t.currentTarget.id;
        this.setData({
            currentColor: e
        });
    },
    choosePencil: function() {
        this.isClear = !1, this.setData({
            size: 3,
            drawStyle: "pencil"
        });
    },
    choosePen: function() {
        this.isClear = !1, this.setData({
            size: 10,
            drawStyle: "pen"
        });
    },
    chooseBrush: function() {
        this.isClear = !1, this.setData({
            size: 20,
            drawStyle: "brush"
        });
    },
    chooseEraser: function() {
        this.isClear = !0, this.setData({
            drawStyle: "eraser"
        });
    },
    touchStart: function(t) {
        this.startX = t.changedTouches[0].x, this.startY = t.changedTouches[0].y, this.context = wx.createContext(), 
        this.isClear ? (this.context.setStrokeStyle("#FFFFFF"), this.context.setLineCap("round"), 
        this.context.setLineJoin("round"), this.context.setLineWidth(20), this.context.save(), 
        this.context.beginPath(), this.context.arc(this.startX, this.startY, 5, 0, 2 * Math.PI, !0), 
        this.context.fill(), this.context.restore()) : (this.context.setStrokeStyle(this.data.currentColor), 
        this.context.setLineWidth(this.data.size), this.context.setLineCap("round"), this.context.beginPath());
    },
    touchMoves: function(t) {
        var e = t.changedTouches[0].x, s = t.changedTouches[0].y;
        this.isClear ? (this.context.save(), this.context.moveTo(this.startX, this.startY), 
        this.context.lineTo(e, s), this.context.stroke(), this.context.restore(), this.startX = e, 
        this.startY = s) : (this.context.moveTo(this.startX, this.startY), this.context.lineTo(e, s), 
        this.context.stroke(), this.startX = e, this.startY = s), wx.drawCanvas({
            canvasId: "myCanvas",
            reserve: !0,
            actions: this.context.getActions()
        });
    },
    touchEnd: function() {},
    savePaint: function() {
        var t = this;
        wx.showActionSheet({
            itemList: [ "保存涂鸦", "发布涂鸦" ],
            success: function(e) {
                0 == e.tapIndex ? t.save() : t.publishPainting();
            },
            fail: function(t) {
                console.log(t.errMsg);
            }
        });
    },
    save: function() {
        var t = wx.getSystemInfoSync().windowWidth, e = wx.getSystemInfoSync().windowHeight;
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: .9 * t,
            height: e,
            destWidth: .9 * t * 2,
            destHeight: 2 * e,
            canvasId: "myCanvas",
            success: function(t) {
                wx.saveImageToPhotosAlbum({
                    filePath: t.tempFilePath
                });
            }
        });
    },
    publishPainting: function() {
        var t = wx.getSystemInfoSync().windowWidth, e = wx.getSystemInfoSync().windowHeight;
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: .9 * t,
            height: e,
            destWidth: .9 * t * 2,
            destHeight: 2 * e,
            canvasId: "myCanvas",
            success: function(t) {
                wx.navigateTo({
                    url: "/pages/sharePainting/sharePainting?imageUrl=" + t.tempFilePath
                });
            }
        });
    }
});